#!/usr/bin/env bash

# PACKAGE: Gunther
# VERSION: 2.0
# DATE: 12/29/08
# AUTHOR(S): Zarrar Shehzad

# THIS REQUIRES THE LINEAR REGISTRATION TO HAVE BEEN RUN!!!

# COMMANDS
declare -a COMMANDS
declare -a COMMANDS_INFO
declare -a COMMANDS_INPUT
declare -a COMMANDS_OUTPUT

# Initial Input and final output
BIG_INPUT="${mprage} ${FSLDIR}/data/standard/MNI152_T1_2mm.nii.gz ${FSLDIR}/data/standard/MNI152_T1_2mm_brain_mask_dil.nii.gz ${regDir}/highres2standard.mat ${regDir}/example_func2standard.mat"
BIG_OUTPUT="${regDir}/fnirt_example_func2standard.${gOUTPUT_TYPE}"

# 1. SETUP ANATOMICAL
# Go into the anat directory and get the head
COMMANDS[1]="cd ${anatDir}"
# Deoblique
COMMANDS_INFO[2]="deobliquing anatomical"
COMMANDS[2]="3dcopy ${mprage} tmp_anat_do.${gOUTPUT_TYPE}"
COMMANDS_OUTPUT[2]="tmp_anat_do.${gOUTPUT_TYPE} head.${gOUTPUT_TYPE}"
COMMANDS[3]="3drefit -deoblique tmp_anat_do.${gOUTPUT_TYPE}"
COMMANDS_OUTPUT[3]="head.${gOUTPUT_TYPE}"
# Reorient
COMMANDS_INFO[4]="reorienting anatomical"
COMMANDS[4]="3dresample -orient RPI -inset tmp_anat_do.${gOUTPUT_TYPE} -prefix head.${gOUTPUT_TYPE}"
COMMANDS_OUTPUT[4]="head.${gOUTPUT_TYPE}"
# Clean up
COMMANDS[5]="rm tmp_anat_do.${gOUTPUT_TYPE} 2> /dev/null; echo -n ''"

# 2. Copy over data
COMMANDS_INFO[6]="copying over (1) mprage, (2) standard head, (3) standard mask"
COMMANDS[6]="cd ${regDir}"
COMMANDS[7]="fslmaths ${anatDir}/head.${gOUTPUT_TYPE} highres_head"
COMMANDS_OUTPUT[7]="highres_head.${gOUTPUT_TYPE}"
COMMANDS[8]="fslmaths ${FSLDIR}/data/standard/MNI152_T1_2mm standard_head"
COMMANDS_OUTPUT[8]="standard_head.${gOUTPUT_TYPE}"
COMMANDS[9]="fslmaths ${FSLDIR}/data/standard/MNI152_T1_2mm_brain_mask_dil standard_mask"
COMMANDS_OUTPUT[9]="standard_mask.${gOUTPUT_TYPE}"

# 3. Perform nonlinear registraion (higres to standard)
COMMANDS[10]="fnirt --in=highres_head --aff=highres2standard.mat --cout=highres2standard_warp --iout=fnirt_highres2standard --jout=highres2standard_jac --config=T1_2_MNI152_2mm --ref=standard_head --refmask=standard_mask --warpres=10,10,10"
COMMANDS_OUTPUT[10]="fnirt_highres2standard.${gOUTPUT_TYPE}"
## pretty pictures
COMMANDS[11]="slicer fnirt_highres2standard standard -s 2 -x 0.35 sla.png -x 0.45 slb.png -x 0.55 slc.png -x 0.65 sld.png -y 0.35 sle.png -y 0.45 slf.png -y 0.55 slg.png -y 0.65 slh.png -z 0.35 sli.png -z 0.45 slj.png -z 0.55 slk.png -z 0.65 sll.png"
COMMANDS[12]="pngappend sla.png + slb.png + slc.png + sld.png + sle.png + slf.png + slg.png + slh.png + sli.png + slj.png + slk.png + sll.png fnirt_highres2standard1.png"
COMMANDS[13]="slicer standard fnirt_highres2standard -s 2 -x 0.35 sla.png -x 0.45 slb.png -x 0.55 slc.png -x 0.65 sld.png -y 0.35 sle.png -y 0.45 slf.png -y 0.55 slg.png -y 0.65 slh.png -z 0.35 sli.png -z 0.45 slj.png -z 0.55 slk.png -z 0.65 sll.png"
COMMANDS[14]="pngappend sla.png + slb.png + slc.png + sld.png + sle.png + slf.png + slg.png + slh.png + sli.png + slj.png + slk.png + sll.png fnirt_highres2standard2.png"
COMMANDS[15]="pngappend fnirt_highres2standard1.png - fnirt_highres2standard2.png fnirt_highres2standard.png"
COMMANDS[16]="rm -f sl?.png fnirt_highres2standard?.png"

# Apply nonlinear registration (func to standard)
COMMANDS[17]="applywarp --ref=standard --in=example_func --out=fnirt_example_func2standard --warp=highres2standard_warp --premat=example_func2highres.mat"
COMMANDS_OUTPUT[17]="fnirt_example_func2standard.${gOUTPUT_TYPE}"
## pretty pictures
COMMANDS[18]="slicer fnirt_example_func2standard standard -s 2 -x 0.35 sla.png -x 0.45 slb.png -x 0.55 slc.png -x 0.65 sld.png -y 0.35 sle.png -y 0.45 slf.png -y 0.55 slg.png -y 0.65 slh.png -z 0.35 sli.png -z 0.45 slj.png -z 0.55 slk.png -z 0.65 sll.png"
COMMANDS[19]="pngappend sla.png + slb.png + slc.png + sld.png + sle.png + slf.png + slg.png + slh.png + sli.png + slj.png + slk.png + sll.png fnirt_example_func2standard1.png"
COMMANDS[20]="slicer standard fnirt_example_func2standard -s 2 -x 0.35 sla.png -x 0.45 slb.png -x 0.55 slc.png -x 0.65 sld.png -y 0.35 sle.png -y 0.45 slf.png -y 0.55 slg.png -y 0.65 slh.png -z 0.35 sli.png -z 0.45 slj.png -z 0.55 slk.png -z 0.65 sll.png"
COMMANDS[21]="pngappend sla.png + slb.png + slc.png + sld.png + sle.png + slf.png + slg.png + slh.png + sli.png + slj.png + slk.png + sll.png fnirt_example_func2standard2.png"
COMMANDS[22]="pngappend fnirt_example_func2standard1.png - fnirt_example_func2standard2.png fnirt_example_func2standard.png"
COMMANDS[23]="rm -f sl?.png fnirt_example_func2standard?.png"

# 5. Copy things to check into the checks folder
COMMANDS_INFO[24]="Copying images into ${checksDir}/2_reg for user to CHECK DATA"
COMMANDS[24]="cp fnirt*.png ${checksDir}/2_reg"
